{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['red', 'yellow', 'blue']\n",
      "['orange', 'green', 'purple']\n"
     ]
    }
   ],
   "source": [
    "# Example 1\n",
    "a = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']\n",
    "odds = a[::2]\n",
    "evens = a[1::2]\n",
    "print(odds)\n",
    "print(evens)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### reversing\ta\tbyte\tstring"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'esoognom'\n"
     ]
    }
   ],
   "source": [
    "# Example 2\n",
    "x = b'mongoose'\n",
    "y = x[::-1]\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### But it does not work with unicode."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Expected\n"
     ]
    }
   ],
   "source": [
    "# Example 3\n",
    "try:\n",
    "    w = '謝謝'\n",
    "    x = w.encode('utf-8')\n",
    "    y = x[::-1]\n",
    "    z = y.decode('utf-8')\n",
    "except:\n",
    "    print('Expected')\n",
    "else:\n",
    "    assert False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'c', 'e', 'g']\n",
      "['h', 'f', 'd', 'b']\n"
     ]
    }
   ],
   "source": [
    "# Example 4\n",
    "a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n",
    "print(a[::2])   # select every 2nd from beginning  \n",
    "print(a[::-2])  # select every 2nd from the end (reverse)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How about using them all? Confuding. Please DON'T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['c', 'e', 'g']\n",
      "['g', 'e', 'c', 'a']\n",
      "['g', 'e']\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# Example 5\n",
    "print(a[2::2])     # ['c', 'e', 'g']\n",
    "print(a[-2::-2])   # ['g', 'e', 'c', 'a']\n",
    "print(a[-2:2:-2])  # ['g', 'e']\n",
    "print(a[2:2:-2])   # []"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### If\tyou must\tuse\ta\tstride,\tprefer\tmaking\tit\ta\tpositive\tvalue\tand\tomit\tstart\tand\tend\tindexes. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n",
      "['a', 'c', 'e', 'g']\n",
      "['c', 'e']\n"
     ]
    }
   ],
   "source": [
    "# Example 6\n",
    "b = a[::2]   # ['a', 'c', 'e', 'g']\n",
    "c = b[1:-1]  # ['c', 'e']\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
